package com.gordon.tips.util;

import javax.servlet.ServletContext;
import org.springframework.web.context.WebApplicationContext;


/**
 *****************************************************************************
 ** Module : Spring bean simple application
 ** 
 ** Date:  Aug 14, 2011
 ** 
 ** Author: Gordon
 ** 
 ** Email: 462991284@qq.com
 ** 
 ** (C) Copyright 2011 Gordon's Office
 *****************************************************************************
 */
public class SpringUtil {

	private static WebApplicationContext ctx;

	public static void setCtx(WebApplicationContext ctx) {
		SpringUtil.ctx = ctx;
	}

	public static WebApplicationContext getCtx() {
		return ctx;
	}

	@SuppressWarnings("unchecked")
	public static <T> T getBean(String name, Class<T> clazz) {
		return (T) ctx.getBean(name, clazz);
	}

	public static Object getBean(String name) {
		return ctx.getBean(name);
	}


	public static Object getBean(ServletContext servletContext, String beanName) {
		WebApplicationContext context = (WebApplicationContext) servletContext
				.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
		return context.getBean(beanName);
	}
}